﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;
using hbse.DTO;
using hbse.Service;

namespace hbse
{
    public partial class Ussimplefloder : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private Users users;
        private SampleFolderMService sampleFolderService = new SampleFolderMService();
        private UntilService untilService = new UntilService();
        private Customer customer;
        private Users user;
        private Product product;


        public Ussimplefloder(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session, Users _users)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _users;
             
        }

        private void Ussimplefloder_Load(object sender, EventArgs e)
        {

        }

        private void cmdsearchproduct_Click(object sender, EventArgs e)
        {
            List<ProductDTO> productDTOs = untilService.getProductsByCode(txtSearch.Text.Trim(), session);
            if (productDTOs.Count > 0)
            {
                dgvProduct.DataSource = productDTOs;
                FormatdgvProcuct();
            }
            else
            {
                dgvProduct.DataSource = null;
                FormatdgvProcuct();
                 
            }
            
        }

        private void FormatdgvProcuct()
        {
            if (dgvProduct.RowCount > 0)
            {
                dgvProduct.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct.Columns["Id"].Visible = false;

            }
        }

        private void dgvProduct_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

          

            txtCode.Text = dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString();
            product = sampleFolderService.getByCode(dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString(),session);


            if (product.ProductType =="0" || product.ProductType =="1"){

                rdbItem.Checked = true;
                cmdsearchsample.Enabled = false;
                txtProductCode2.Text = "";

            }else {
                
                rdbSet.Checked = true;
                cmdsearchsample.Enabled = true;
                txtProductCode2.Text = product.ProductType;
            }
           

            txtac.Text = product.ProductAc;
            txtar.Text = product.ProductAr;
            txtgab.Text = product.ProductGab;
            txtdesc.Text = product.ProductDescription;
            txtCustomerCode.Text = product.Customer.CustomerCode;
            txtcustomerName.Text = product.Customer.CustomerName;
            txtAdditional.Text = product.AdditionalProcess;
            customer = untilService.getCustomerbyCode(product.Customer.CustomerCode, session);

        }

        private void cmdsearchcustomer_Click(object sender, EventArgs e)
        {
            panel2.Show();
            List<hbse.DTO.CustomerDTO> customerDTOs = untilService.getCustomersAll(session);
            if (customerDTOs.Count > 0)
            {
                dgvCustomer.DataSource = customerDTOs;
                FormatdgvCustomer();
            }
            else
            {
                dgvCustomer.DataSource = null;


            }

        }

        private void FormatdgvCustomer()
        {
            if (dgvCustomer.RowCount > 0)
            {
                dgvCustomer.Columns["customerCode"].HeaderText = "customerCode";
                dgvCustomer.Columns["customerName"].HeaderText = "customerName";

                dgvCustomer.Columns["Id"].Visible = false;

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            panel1.Hide();
        }

        private void cmdsearchsample_Click(object sender, EventArgs e)
        {

            panel1.Show();

            List<hbse.DTO.ProductDTO> productDTOs = untilService.getProductsByCodeT10(txtCode.Text.Trim(), session);
            if (productDTOs.Count > 0)
            {
                dgvProduct2.DataSource = productDTOs;
                FormatdgvProcuct2();
            }
            else
            {
                dgvProduct2.DataSource = null;


            }
        }
        private void FormatdgvProcuct2()
        {
            if (dgvProduct2.RowCount > 0)
            {
                dgvProduct2.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct2.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct2.Columns["Id"].Visible = false;

            }
        }
         
        private void rdbItem_Click(object sender, EventArgs e)
        {
            rdbSet.Checked = false;
            cmdsearchsample.Enabled = false;
        }

        private void rdbSet_Click(object sender, EventArgs e)
        {
            rdbItem.Checked = false;
            cmdsearchsample.Enabled = true;
        }

        private void dgvProduct2_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            txtProductCode2.Text = dgvProduct2.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString();

            panel1.Hide();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Orange,
            e.ClipRectangle.Left,
            e.ClipRectangle.Top,
            e.ClipRectangle.Width - 1,
            e.ClipRectangle.Height - 1);
                        base.OnPaint(e);

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Orange,
           e.ClipRectangle.Left,
           e.ClipRectangle.Top,
           e.ClipRectangle.Width - 1,
           e.ClipRectangle.Height - 1);
            base.OnPaint(e);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel2.Hide();
        }

        private void dgvCustomer_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
           
            customer = untilService.getCustomerbyCode(dgvCustomer.Rows[e.RowIndex].Cells["CustomerCode"].Value.ToString(),session);

            txtCustomerCode.Text = customer.CustomerCode;
            txtcustomerName.Text = customer.CustomerName;
            panel2.Hide();
        }

        private void cmdadd_Click(object sender, EventArgs e)
        {

            Product oldproduct = sampleFolderService.getByCode(txtCode.Text.Trim(), session);
            if (oldproduct.ProductCode != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("รหัสนี้มีอยู่แล้วก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCode.Focus();
                return;
            }

            if (txtCode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCode.Focus();
                return;
            }
            if (txtcustomerName.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcustomerName.Focus();
                return;
            }

            Product newProduct = new Product();

            newProduct.ProductCode = txtCode.Text.Trim();
            newProduct.ProductAc = txtac.Text.Trim();
            newProduct.ProductAr = txtar.Text.Trim();
            newProduct.ProductGab = txtgab.Text.Trim();
            newProduct.ProductDescription = txtdesc.Text.Trim();
            newProduct.Customer = customer;
            newProduct.Users = users;
            newProduct.AdditionalProcess = txtAdditional.Text.Trim();
            if (rdbItem.Checked == true)
            {
                newProduct.ProductType = "0";

            }
            else {
                newProduct.ProductType = txtProductCode2.Text.Trim();
                Product mproduct = sampleFolderService.getByCode(txtProductCode2.Text.Trim(), session);
                mproduct.ProductType = "1";
                int mresult = sampleFolderService.editProduct(mproduct, session);
                if (mresult > -1)
                {
                    Console.WriteLine("Update Complete");
                    //MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    Console.WriteLine("Update Not Complete");
                    //MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }


            }
            newProduct.AddDate = DateTime.Now;
            newProduct.Status = "1";

            int result = sampleFolderService.AddProduct(newProduct,session);
            if (result > -1)
            {
                Console.WriteLine("Add Complete");
                MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                List<ProductDTO> productDTOs = untilService.getProductsByCode(txtSearch.Text.Trim(), session);
                if (productDTOs.Count > 0)
                {
                    dgvProduct.DataSource = productDTOs;
                    FormatdgvProcuct();
                }
                else
                {
                    dgvProduct.DataSource = null;
                    FormatdgvProcuct();

                }

            }
            else
            {
                Console.WriteLine("add Not Complete");
                //MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            // add print 
            List<ProductPrint> productPrints = new List<ProductPrint>();

            ProductPrint productPrint = new ProductPrint();
            productPrint.Users = users;
            productPrint.Product = newProduct;
            productPrint.NumPrint = 1;
            productPrint.PrintDate = DateTime.Now;
            productPrints.Add(productPrint);

            newProduct.ProductPrints = productPrints;


            int resultprint = sampleFolderService.editProduct(newProduct, session);
            if (resultprint > -1)
            {
                Console.WriteLine("Add Complete");
                //MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                Console.WriteLine("add Not Complete");
                //MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



        }

        private void cmdedit_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCode.Focus();
                return;
            }

            if (txtcustomerName.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcustomerName.Focus();
                return;
            }

            product.ProductCode = txtCode.Text.Trim();
            product.ProductAc = txtac.Text.Trim();
            product.ProductAr = txtar.Text.Trim();
            product.ProductGab = txtgab.Text.Trim();
            product.ProductDescription = txtdesc.Text.Trim();
            product.Customer = customer;
            product.Users = user;
            product.AdditionalProcess = txtAdditional.Text.Trim();
            if (rdbItem.Checked == true)
            {
                product.ProductType = "0";

            }
            else
            {
                product.ProductType = txtProductCode2.Text.Trim();
            }
            product.AddDate = DateTime.Now;
            product.Status = "1";

            int result = sampleFolderService.editProduct(product, session);
            if (result > -1)
            {
                Console.WriteLine("Update Complete");
                //MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                List<ProductDTO> productDTOs = untilService.getProductsByCode(txtSearch.Text.Trim(), session);
                if (productDTOs.Count > 0)
                {
                    dgvProduct.DataSource = productDTOs;
                    FormatdgvProcuct();
                }
                else
                {
                    dgvProduct.DataSource = null;
                    FormatdgvProcuct();

                }

            }
            else
            {
                Console.WriteLine("Update Not Complete");
                //MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void cmedelete_Click(object sender, EventArgs e)
        {
            if (txtCode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCode.Focus();
                return;
            }

             

            product.Status = "2";

            int result = sampleFolderService.delProduct(product, session);
            if (result > -1)
            {
                Console.WriteLine("Delelte Complete");
                //MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                List<ProductDTO> productDTOs = untilService.getProductsByCode(txtSearch.Text.Trim(), session);
                if (productDTOs.Count > 0)
                {
                    dgvProduct.DataSource = productDTOs;
                    FormatdgvProcuct();
                }
                else
                {
                    dgvProduct.DataSource = null;
                    FormatdgvProcuct();

                }

            }
            else
            {
                Console.WriteLine("Delete Not Complete");
                //MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void cmdsearch2_Click(object sender, EventArgs e)
        {
         
            List<hbse.DTO.ProductDTO> productDTOs = untilService.getProductsByCodeT10(txtsearch2.Text.Trim(), session);
            if (productDTOs.Count > 0)
            {
                dgvProduct2.DataSource = productDTOs;
                FormatdgvProcuct2();
            }
            else
            {
                dgvProduct2.DataSource = null;


            }
        }

        private void cmscussearch_Click(object sender, EventArgs e)
        {
            List<hbse.DTO.CustomerDTO> customerDTOs = untilService.getCustomersByCode(txtcussearch.Text.Trim(), session);
            if (customerDTOs.Count > 0)
            {
                dgvCustomer.DataSource = customerDTOs;
                FormatdgvCustomer();
            }
            else
            {
                dgvCustomer.DataSource = null;


            }

        }

         
    }
}
